package com.tank.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.tank.bean.item.Image;
import com.tank.dao.ImageDao;

@Repository("imageDao")
public class ImageDaoImpl extends TankDaoSupport implements ImageDao{
	private static Log log = LogFactory.getLog(ImageDaoImpl.class);
	
	public void addIamge(Image image){
		this.getHibernateTemplate().save(image);
	}

	public List<Image> imageForPreview(String itemTag) {
		List<Image> images = new ArrayList<Image>();
		StringBuffer hql = new StringBuffer();
		hql.append("from Image im where im.itemTag='"+itemTag+"'");
		log.debug("imageForPreview HQL:"+hql.toString());
		images = this.getHibernateTemplate().find(hql.toString());
		return images;
	}

	public void deleteTagImage(Integer imageid) {
		Session session = this.getSession();
		StringBuffer sql = new StringBuffer();
		sql.append("delete from image where id="+imageid);
		log.debug("imageForPreview sql:"+sql.toString());
		Query query = session.createSQLQuery(sql.toString());
		query.executeUpdate();
	}

	public int nodifImagePreviw(Integer imageid) {
		// TODO Auto-generated method stub
		return 0;
	}

	public List<Image> queryImagesById(long productId) {
		List<Image> images = new ArrayList<Image>();
		StringBuffer hql = new StringBuffer();
		hql.append("from Image im where im.item.id="+productId);
		log.debug("imageForPreview HQL:"+hql.toString());
		images = this.getHibernateTemplate().find(hql.toString());
		return images;
	}

}
